package com.hqj.zuul.config;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
//@Component
public class LoggerFilter extends ZuulFilter {
    /**
     * 过滤的方式
     * @return
     * pre - 前置过滤
     * route - 路由过滤
     * error - 异常过滤
     * post - 远程服务调用过滤
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 排序过滤
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 表示是否开启过滤
     * @return
     * true 开启   false 关闭
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 具体的过滤逻辑运算
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        //获取请求路径的上下文
        RequestContext rc = RequestContext.getCurrentContext();
        HttpServletRequest req = rc.getRequest();
        String methodName = req.getMethod();
        String url = req.getRequestURL().toString();
        System.out.println("url="+url+",method="+methodName);
        return null;
    }
}
